﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Configuration;

namespace Final.App_Code
{
    [DataObject]
    public class Subject_Desc
    {
        private string subjectcode;
        private string subjectname;
        private string subjectdesc;
               
        [DataObjectField(true, true)]
        public string Subjectcode
        {
            get { return subjectcode; }
            set { subjectcode = value;}          
        }
        public string Subjectdesc
        {
            get { return subjectdesc; }
            set { subjectdesc = value;}                  
        }
        public string Subjectname
        {
            get { return subjectname; }
            set { subjectname = value;}          
        }
        
        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<Subject_Desc> GetSubjectName()
        {
            SqlConnection conn = null;
            List<Subject_Desc> results = new List<Subject_Desc>();
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["SubjectManagementDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select Subject_Code,Subject_Name FROM Subj_Desc";
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    Subject_Desc s = new Subject_Desc();
                    s.subjectcode = (string)dr["Subject_Code"];
                    s.subjectname = (string)dr["Subject_Name"];

                    results.Add(s);
                }

                dr.Close();
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return results;
        }

        //[DataObjectMethod(DataObjectMethodType.Select)]
        //public static DataTable GetSubjectDescription(String subjectcode)
        //{
        //    SqlConnection conn = null; 
        //    DataTable dt = new DataTable();
        
        //    try
        //    {     
        //        conn = new SqlConnection();
        //        conn.ConnectionString = ConfigurationManager.ConnectionStrings["SubjectManagementDBString"].ConnectionString;
        //        conn.Open();
        //        SqlCommand comm = new SqlCommand();
        //        comm.Connection = conn;
        //        comm.CommandText = "Select Subject_Desc from Subj_Desc where Subject_Code = @subjectcode";
        //        comm.Parameters.AddWithValue("@subjectcode", subjectcode);
        //        SqlDataReader dr = comm.ExecuteReader();

        //        dt.Load(dr);
               
        //        dr.Close();
        //    }
        //    catch (SqlException ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        conn.Close();
        //    }
        //    return dt;      
        //}

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable getallsubject()
        {
            SqlConnection conn = null;
            DataTable dt = new DataTable();

            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["SubjectManagementDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from Subj_Desc";
                //comm.Parameters.AddWithValue("@subjectcode", subjectcode);
                SqlDataReader dr = comm.ExecuteReader();

                dt.Load(dr);

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        //[DataObjectMethod(DataObjectMethodType.Delete, true)]
        //public static void DeleteSubject(Subject_Desc k)
        //{
        //    SqlConnection conn = null;

        //    try
        //    {
        //        conn = new SqlConnection();
        //        conn.ConnectionString = ConfigurationManager.ConnectionStrings["SubjectManagementDBString"].ConnectionString;
        //        SqlCommand comm = new SqlCommand();
        //        conn.Open();
        //        comm.Connection = conn;
        //        comm.CommandText = "DELETE FROM Subj_Desc WHERE Subject_Code = @subjectcode";
        //        comm.Parameters.AddWithValue("@subjectcode",k.subjectcode);
        //        int rowsAffected = comm.ExecuteNonQuery();
        //    }
        //    catch (SqlException e)
        //    {
        //        throw e;
        //    }
        //    finally
        //    {
        //        conn.Close();
        //    }
        //}
    }
 }


        
